<?php
/*
 * @Author: your name
 * @Date: 2020-05-22 16:41:13
 * @LastEditTime: 2020-05-27 11:20:30
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /v3/application/adminapiv3/service/userManage/UserService.php
 */ 
namespace app\adminapiv3\service\userManage;

use app\adminapiv3\service\Base;

class UserService extends Base
{
    /**
     * @name: 列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-05-22 16:42:12
     */
    function list ($params, $userGroupId)
    {
        $user = $this->getUserInfo();
        $fields = 'id, show_name, email, real_name, reg_ip, ctime, last_login_ip, last_login_time';

        $db = db('ServerUser')->field($fields)->where('user_group_id', $userGroupId);

        if(isset($params['show_name']) && !empty($params['show_name'])){
            $db->where('show_name', 'like', '%'.$params['show_name'].'%');
        }
        if(isset($params['real_name']) && !empty($params['real_name'])){
            $db->where('real_name', 'like', '%'.$params['real_name'].'%');
        }
        if(isset($params['email']) && !empty($params['email'])){
            $db->where('email', 'like', '%'.$params['email'].'%');
        }
        if(isset($params['last_login_ip']) && !empty($params['last_login_ip'])){
            $db->where('last_login_ip', 'like', '%'.$params['last_login_ip'].'%');
        }
        if (isset($filterData['last_login_time']) && !empty($filterData['last_login_time']))
        {
            $stime = strtotime($filterData['last_login_time']);
            $etime = $stime + 86400;
            $db->whereBetween('last_login_time', [$stime,$etime]);
        }

        $return = $db->paginate(getLimits($page, $limit))->toArray();



        foreach ($return['data'] as $k => $v){
            $return['data'][$k]['is_self'] = $user['user_id'] == $v['id'] ? 1 : 0;
            $return['data'][$k]['is_admin'] = $v['id'] == 1 ? 1 : 0;
            $return['data'][$k]['last_login_time'] = $v['last_login_time'] == 0 ? '暂未登录' : date('Y-m-d H:i:s', $v['last_login_time']);
        }

        //用户组详情
        $groupInfo = db('server_user_group')->where('id', $userGroupId)->find();
        if($groupInfo['level'] == 0 && $groupInfo['upper_id'] == 0){
            $groupInfo['company'] = db('server_auth')->value('unit_name');
        }
        $return['group_info'] = $groupInfo;


        return $return;
    }

    /**
     * @name: 重置密码 
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-05-22 17:38:40
     */
    function resetPassword ($user_id, $password, $confirm_password)
    {
        $user = $this->getUserInfo();

        $userInfo = db('server_user')->alias('u')
            ->join('server_user_group g', 'u.user_group_id = g.id')
            ->where('u.id', $user_id)->find();


        //可以对当前用户(自己)做操作
        if($user_id != $user['user_id']){
            $this->checkLevel($userInfo['level'], $user['user_group_level']);
        }



        if ($password != $confirm_password)
        {
            $this->setError($this->CODE_FAIL, '请确认密码');
            return false;
        }
        
        $password = password_hash($password, PASSWORD_DEFAULT);
        
        db('ServerUser')->where('id', $user_id)->update(['password' => $password]);
    }

    /**
     * @name: 删除用户
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-05-23 15:10:07
     */
    function delete ($user_id)
    {
        $user = $this->getUserInfo();
        $userInfo = db('server_user')->alias('u')
            ->join('server_user_group g', 'u.user_group_id = g.id')
            ->where('u.id', $user_id)->find();
        $this->checkLevel($userInfo['level'], $user['user_group_level']);

        db('ServerUser')->where('id', $user_id)->delete($user_id);
    }

    /**
     * @name: 修改用户信息
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-05-23 15:19:38
     */
    function modify ($user_id, $show_name, $real_name, $email)
    {

        //判断用户组等级
        $user = $this->getUserInfo();
        $userInfo = db('server_user')->alias('u')
            ->join('server_user_group g', 'u.user_group_id = g.id')
            ->where('u.id', $user_id)->find();

        //可以对当前用户(自己)做操作
        if($user_id != $user['user_id']){
            $this->checkLevel($userInfo['level'], $user['user_group_level']);
        }

        //判断名称重复
        $count = db('ServerUser')->where('real_name', $real_name)
            ->where('id','<>', $user_id)
            ->count();

        if($count){
            $this->setError($this->CODE_FAIL, '已存在该用户名');
            return [];
        }
        
        db('ServerUser')->update([
            'id' => $user_id,
            'show_name' => $show_name,
            'real_name' => $real_name,
            'email' => $email
        ]);
    }

    /**
     * @name: 添加用户
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-05-27 10:53:24
     */
    function add ($input)
    {
        //判断用户组等级
        $user = $this->getUserInfo();
        $userGroupInfo = db('server_user_group')->where('id', $input['user_group_id'])->find();
        $this->checkLevel($userGroupInfo['level'], $user['user_group_level']);

        if(db('ServerUser')->where('real_name', $input['real_name'])->count())
        {
            jsonErrorOut('已存在该用户名', $this->CODE_FAIL);
        }

        $input['fid'] = $user['user_id'];
        $input['ctime'] = time();
        $input['password'] = password_hash($input['password'], PASSWORD_DEFAULT);
        db('ServerUser')->insert($input);
    }

    /**
     * @name 检查是否是越级管理
     * @param $groupLevel 用户组等级
     * @param $nowUserLevel 当前操作用户等级
     * @author wx
     * @date 2020/7/20 11:29
     */
    function checkLevel($groupLevel , $nowUserLevel)
    {

        if($nowUserLevel + 1 != $groupLevel){
            returnErrJson($this->CODE_FAIL, '你不能对当前用户组做此操作');
        }
    }
}
